package com.example.task1.ui.AllService.logistics;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.LogisticsBannerBean;
import com.example.task1.bean.LogisticsListBean;
import com.example.task1.bean.LogisticsOrderBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.youth.banner.Banner;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class LogisticsActivity extends AppCompatActivity {

    private EditText etFind;
    private Banner banner;
    private RecyclerView rvList;
    private LogisticsListAdapter logisticsListAdapter;
    private LinearLayoutManager linearLayoutManager;
    private int pageNum = 1;
    private boolean isTrue = true;
    private void loadCommentList() {
        rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (logisticsListAdapter.getItemCount() - 1 == linearLayoutManager.findLastVisibleItemPosition() && isTrue) {
                    isTrue = false;
                    getRvList();
                }
            }
        });
    }
    private static final String TAG = "LogisticsActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("物流查询");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        search();
    }

    private void search() {
        etFind.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String find = etFind.getText().toString();
                    Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN ,"/prod-api/api/logistics-inquiry/logistics_info/" + find);
                    Api.send(apiRespCall, LogisticsOrderBean.class, new CallBack<LogisticsOrderBean>() {
                        @Override
                        public void success(LogisticsOrderBean data) {
                            etFind.setText("");
                            MyApplication.logisticsOrderBean = data;
                            etFind.setFocusable(false);
                            MyApplication.intent(LogisticsActivity.this,LogisticsOrderActivity.class);
                        }

                        @Override
                        public void error(String msg) {
                        }
                    });
                }
                return false;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData() {
        Call<ApiResp> bannerCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/logistics-inquiry/ad-banner/list");
        Api.send(bannerCall, LogisticsBannerBean.class, new CallBack<LogisticsBannerBean>() {
            @Override
            public void success(LogisticsBannerBean data) {
                List<String> rows = data.getData().stream().map(LogisticsBannerBean.DataDTO::getImgUrl).collect(Collectors.toList());
                MyApplication.banner(banner, rows);
            }

            @Override
            public void error(String msg) {

            }
        });
        getRvList();
        setAdapter();
        loadCommentList();
    }



    private void setAdapter() {
        logisticsListAdapter = new LogisticsListAdapter(this);
        linearLayoutManager = new LinearLayoutManager(this);
        rvList.setAdapter(logisticsListAdapter);
        rvList.setLayoutManager(linearLayoutManager);
    }

    private void getRvList() {
        Call<ApiResp> rvCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/logistics-inquiry/logistics_company/list?pageSize=10&pageNum=" + pageNum);
        Api.send(rvCall, LogisticsListBean.class, new CallBack<LogisticsListBean>() {
            @Override
            public void success(LogisticsListBean data) {
                List<Integer> oldIds = logisticsListAdapter.getData().stream().map(LogisticsListBean.DataDTO::getId).collect(Collectors.toList());
                List<Integer> newIds = data.getData().stream().map(LogisticsListBean.DataDTO::getId).collect(Collectors.toList());
                if (!oldIds.containsAll(newIds)) {
                    isTrue  = true;
                    logisticsListAdapter.setData(data.getData());
                    pageNum++;
                }else {
                    MyApplication.show("没有更多数据了");
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void initView() {
        etFind = (EditText) findViewById(R.id.et_find);
        banner = (Banner) findViewById(R.id.banner);
        rvList = (RecyclerView) findViewById(R.id.rv_list);
    }
}